#ifndef QMLHANDLER_H
#define QMLHANDLER_H

#include <QtGui/QApplication>
#include <QObject>
#include <QDeclarativeContext>
#include <QTranslator>
#include "baza/baza.h"
#include "baza/popis.h"
#include "itemalllists.h"

#include "qmessage.h"
#include "qmessageservice.h"

#include "languageitem.h"
#include "languagemodel.h"

#include "groupitem.h"
#include "groupmodel.h"

#include "listitem.h"
#include "listmodel.h"

#include "itemitem.h"
#include "itemmodel.h"

QTM_USE_NAMESPACE

class QMLHandler : public QObject
{
    Q_OBJECT
public:
    explicit QMLHandler(QObject *parent = 0);
    QMLHandler(Baza *database, QDeclarativeContext *ctxt, QApplication *app);
    ~QMLHandler();

public:
    Q_INVOKABLE bool allLists_Add(QString name);
    Q_INVOKABLE void allLists_Delete(int index);
    Q_INVOKABLE void allLists_MoveUp(int index);
    Q_INVOKABLE void allLists_MoveDown(int index);
    Q_INVOKABLE void allLists_Message(int popisIndex);
    Q_INVOKABLE bool allLists_Rename(QString name, int index);
    Q_INVOKABLE void allLists_DeleteBought();

    Q_INVOKABLE void settings_SetLanguage(int languageId);

    Q_INVOKABLE void refreshLanguages();
    Q_INVOKABLE int getChosenLanguageIndex();
    Q_INVOKABLE void loadTranslation(QString sifraJezika);

    Q_INVOKABLE void refreshAllLists();

    Q_INVOKABLE void refreshGroups();

    Q_INVOKABLE void refreshCurrentList(int currentListIndex, int groupId);

    Q_INVOKABLE void refreshUnits();

    Q_INVOKABLE void viewList_SetBought(int itemId, int listIndex, bool bought);
    Q_INVOKABLE void viewList_DeleteBought(int listId, int itemId);
    Q_INVOKABLE void viewList_Delete(int listIndex, int itemId, int groupId);
    Q_INVOKABLE void viewList_SetQuantity(int listIndex, int itemId, float quantity, QString unit);

    Q_INVOKABLE bool editGroups_Add(QString name);
    Q_INVOKABLE void editGroups_Delete(int groupId);
    Q_INVOKABLE void editGroups_ChangePriority(int index_1, int index_2);
    Q_INVOKABLE bool editGroups_Rename(QString name, int groupId);

    Q_INVOKABLE bool editItems_Add(QString name, int groupIdItem, int groupIndexRefresh);
    Q_INVOKABLE void editItems_Delete(int itemId, int groupIndex);
    Q_INVOKABLE bool editItems_Rename(QString name, int itemId);
    Q_INVOKABLE void editItems_ChangeGroup(int itemId, int newGroupId, int groupIndexRefresh);

    Q_INVOKABLE void addItem_Add(int itemId, int listIndex, float quantity, QString unit, int groupIndexRefresh);
    Q_INVOKABLE bool addItem_ExistItem(QString name, int listIndex);

    Q_INVOKABLE bool firstTime();
    Q_INVOKABLE void createTables();
    Q_INVOKABLE void createExampleDatabase();

    Q_INVOKABLE void refreshItems(int groupId, int listIndex);
public:

signals:

public slots:

private:
    Baza *database;
    QDeclarativeContext *ctxt;
    QApplication *app;

    //all lists
    std::vector<Popis> listAllLists;
    QList<QObject*> modelAllLists;

    //languages
    LanguageModel modelLanguages;
    int chosenLanguageIndex;

    //groups
    GroupModel modelGroups;
    GroupModel modelGroupsWithoutAllGroups;

    //current list
    ListModel modelList;


    QTranslator translator;

    //units (koristim grupe da ne pisem novi model)
    GroupModel modelUnits;

    ItemModel modelItems;

    //int currentDisplayedListIndex;
};

#endif // QMLHANDLER_H
